#include "DialogSetting.h"
#include "DefineCommon.h"
#include "EItemManager.h"
#include "qtreewidget.h"
#include "ui_DialogSetting.h"

#include <EControllerQtTree.h>
#include <EControllerQtProperty.h>

class EControllerSettingTree;
typedef SmartPtr(EControllerSettingTree) EControllerSettingTreePtr;
class EControllerSettingTree : public EControllerQtTree
{
    virtual void handleCommand(const ECommandPtr& command)
    {
    }
};

class EControllerSettingProperty;
typedef SmartPtr(EControllerSettingProperty) EControllerSettingPropertyPtr;
class EControllerSettingProperty : public EControllerQtProperty
{
    virtual void handleCommand(const ECommandPtr& command)
    {
        EString name = command->getName();
        ECommand::STATE state = command->getState();
        EString senderName = command->getSenderName();

        if(command->getName() == "c:selectItem" 
                && command->getState() == ECommand::STATE::NORMAL
                && command->getSenderName() == "WidgetTreeSetting"
          )
        {
            ELOG_DEBUG("cmd.name=%s, cmd.senderName=%s", command->getName().data(), senderName.data());
            this->handleCommandSelectItem(command);
        }
        else if(command->getName() == "c:propertyChanged" 
                && command->getState() == ECommand::STATE::UNDO
                // && command->getSenderName() == this->getName()
               )
        {
            // this->handleCommandPropertyUndo(command);
        }
        else if(command->getName() == "c:propertyChanged" 
                && command->getState() == ECommand::STATE::REDO
                // && command->getSenderName() == this->getName()
               )
        {
            // this->handleCommandPropertyRedo(command);
        }
        else if(command->getName() == "c:propertyChanged" 
                && command->getState() == ECommand::STATE::NORMAL
                && command->getSenderName() != this->getName()
               )
        {
            // this->handleCommandPropertyChangedByOther(command);
        }
    }
};

DialogSetting::DialogSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetting)
{
    ui->setupUi(this);
    QList<int> splitterSize;
    splitterSize<<1<<4;
    // ui->splitter->setStretchFactor(0, 1);
    // ui->splitter->setStretchFactor(0, 0);
    ui->splitter->setSizes(splitterSize);

    QObject::connect(ui->buttonSave, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonOk(bool ))
                     );
    QObject::connect(ui->buttonCancel, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonCancel(bool ))
                     );
    QObject::connect(ui->treeWidget, 
                     SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
                     this,
                     SLOT(slotCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*))
                     );

    
    m_controllerTree = NewSmartPtr(EControllerQtTree)();
    m_controllerTree->setTreeWidget(ui->treeWidget);
    m_controllerTree->setRootItem(EItemManager::instance()->getItemSettingRoot());
    m_controllerTree->setName("WidgetTreeSetting");
    m_controllerTree->refresh();

    ui->treeWidget->expandAll();

    m_controllerProperty = NewSmartPtr(EControllerSettingProperty)();
    m_controllerProperty->setName("WidgetPropertySetting");
    ui->propertyWidget->setController(m_controllerProperty);

    ui->buttonSave->setEnabled(false);
}

DialogSetting::~DialogSetting()
{
    delete ui;
}



void DialogSetting::slotButtonSave(bool value)
{
    this->m_ifCancelled = false;
    this->accept();
}

void DialogSetting::slotButtonCancel(bool value)
{
    this->m_ifCancelled = true;
    this->accept();
}

bool DialogSetting::getIfCancelled()
{
    return this->m_ifCancelled;
}

void DialogSetting::slotCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
}

